package core.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import core.domain.Dao;
import core.domain.IdEntity;

@Transactional
@Service
public abstract class AbstractService<T extends IdEntity> implements core.service.Service<T> {

	public static final Logger logger = Logger.getLogger(AbstractService.class);
	
	@PersistenceContext
	private EntityManager em;
	
	public EntityManager getEm() {
		return em;
	}

	public void setEm(EntityManager em) {
		this.em = em;
	}
	
	protected abstract Dao<T> getDao();

	public T findObject(long oid) {
		return getDao().findObject(oid);
	}

	public List<T> findObjects(List<Long> oids) {
		return getDao().findObjects(oids);
	}

	public T remove(T entity) {
		return getDao().remove(entity);
	}

	public T store(T entity) {
		logger.info(getEm());
		return getDao().store(entity);
	}

}